#pragma once
#include "header.h"
void loadboard(std::string &tem, std::vector<std::vector<char>> &mine,
               std::vector<std::vector<char>> &show)
{
    // 加载棋盘
    for (auto &e : show)
    {
        for (auto &s : e)
        {
            tem += s;
            tem += " ";
        }
        tem += "\r\n";
    }
}
void cut(std::string cutstring, int &x, int &y)
{
    int pos = cutstring.find("\n");
    if (pos != std::string::npos)
    {
        x = atoi(cutstring.substr(0, pos).c_str());
        y = atoi(cutstring.substr(pos + 1).c_str());
    }
    else
        std::cout << "查找失败" << std::endl;
}
void initboard(std::string &tem, std::vector<std::vector<char>> &mine,
               std::vector<std::vector<char>> &show)
{
    // 初始化棋盘
    show.resize(COW);
    for (auto &e : show)
        e.resize(ROW, '*');
    for (size_t i = 0; i < ROW; i++)
        show[0][i] = (i + 1) + '0';
    for (size_t i = 1; i < COW; i++)
        show[i][0] = (i + 1) + '0';
    mine = show;
    // 埋雷
    for (size_t i = 0; i < MINE;)
    {
        int x = rand() % ROW;
        int y = rand() % COW;
        if (y != 0 && x != 0 && mine[x][y] == '*')
        {
            mine[x][y] = '#';
            i++;
        }
    }
    // 加载
    loadboard(tem, mine, show);
}
void playtwo(int sock)
{
}
void playone(int sock, std::string &board, std::vector<std::vector<char>> &mine,
             std::vector<std::vector<char>> &show)
{
    while (true)
    {
        // 发送棋盘
        send(sock, board.c_str(), board.size(), 0);
        // 接受请求
        char buffer[SIZE_BUFFER];
        memset(buffer, 0, SIZE_BUFFER);
        recv(sock, buffer, SIZE_BUFFER - 1, 0);
        // 分割
        int x;
        int y;
        cut(buffer, x, y);
        // 对比埋雷的棋盘
        if (show[x - 1][y - 1] == mine[x - 1][y - 1] && mine[x - 1][y - 1] == '#')
        {
            std::cout << "炸了，游戏结束" << std::endl;
            break;
        }
        else
        {
            show[x - 1][y - 1] = ' ';
            loadboard(board, mine, show);
            send(sock, board.c_str(), board.size(), 0);
        }
    }
}
// 建立连接
void mnue(std::string &buffer)
{
    srand((unsigned int)time(nullptr));
    buffer = "******************************\r\n";
    buffer += "******************************\r\n";
    buffer += "*************0.退出************\r\n";
    buffer += "*************1.人机模式**********\r\n";
    buffer += "*************2.双人模式*********\r\n";
    buffer += "*******************************\r\n";
    buffer += "*******************************\r\n";
}
void *start(void *args)
{
    // 线程分离
    pthread_detach(pthread_self());
    int sockfd = *(reinterpret_cast<int *>(args));
    // 加载棋盘
    std::vector<std::vector<char>> mine;
    std::vector<std::vector<char>> show;
    std::string board;
    initboard(board, mine, show);
    char bufer[SIZE_BUFFER];
    memset(bufer, 0, SIZE_BUFFER);
    std::string buffer;
    mnue(buffer);
    int input;
    do
    {
        // 发送菜单
        send(sockfd, buffer.c_str(), buffer.size(), 0);
        // 读取选择
        ssize_t s = recv(sockfd, bufer, SIZE_BUFFER, 0);
        if (s < 0)
        {
            std::cout << "recv fail" << std::endl;
            break;
        }
        // 接受请求
        else
            input = atoi(bufer);
        switch (input)
        {
        case 0:
            std::cout << "退出" << std::endl;
            break;
        case 1:
            playone(sockfd, board, mine, show);
            break;
        case 2:
            playtwo(sockfd);
            break;
        default:
            std::cout << "选择错误，请重新选择" << std::endl;
            break;
        }
    } while (input);
    close(sockfd);
    return nullptr;
}